<template>
  <div class="log_manage">
    <div class="head">
      <div class="step">
        <span><i class="step-i el-icon-back" title="上一层" @click="onStep"></i></span>
        <span><i class="step-i el-icon-right" title="下一层" @click="underStep"></i></span>
      </div>
      <div class="path path_display" v-if="is_path" tabindex="-1" @focus="focus_path">
        <i slot="prefix" class="el-input__icon el-icon-folder-opened"
           style="color: #ff9b11; font-size: 22px; margin: 0 5px"></i>
        <span v-for="(value, index) in display_path" :key="index">
          <i class="el-icon-arrow-right"></i>
          {{ value }}
        </span>
      </div>
      <el-input class="path path-input" v-model="log_path" placeholder="请输入内容" v-else
                tabindex="-1" @blur="blur_path" ref="input" @keyup.enter.native="onSubmit">
        <i slot="prefix" class="el-input__icon el-icon-folder-opened"
           style="color: #ff9b11; font-size: 22px; margin: 0 5px"></i>
      </el-input>
      <div class="refresh">
        <el-button icon="el-icon-refresh" @click="refresh_content" title="刷新"></el-button>
      </div>
    </div>
    <div class="content-box">
      <el-table
          :data="dir_list"
          :default-sort="{prop: 'type', order: 'ascending'}"
          :stripe="false"
          :row-class-name="tableRowClassName"
          @row-click="rowClick"
          :row-style="rowStyle"
          @cell-mouse-enter="hoverStyle"
          @row-dblclick="rowDblclick"
          style="width: 100%">
        <el-table-column
            prop="name"
            label="名称"
            sortable
            width="260">
          <template slot-scope="scope">
            <span v-if="scope.row.type === '文件夹'">
              <i class="el-icon-folder" style="color: #ff9b11;"></i>
              {{ scope.row.name }}
            </span>
            <span v-else>
              <i class="el-icon-tickets"></i>
              {{ scope.row.name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column
            prop="time"
            sortable
            label="修改日期"
            width="220">
        </el-table-column>
        <el-table-column
            class="file-type"
            prop="type"
            sortable
            label="类型"
            width="200">
        </el-table-column>
        <el-table-column
            prop="size"
            label="大小"
            width="460">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="my_delete(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="file_content">
      <el-dialog
          title="提示"
          :visible.sync="display_file_status"
          width="50%"
          center>
        <textarea cols="120" rows="30" disabled>
          {{ file_text }}
        </textarea>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {open_dir, open_file, delete_file} from "@/js/log_manage"

export default {
  name: "LogManage",
  data() {
    return {
      // 点击时行索引
      getIndex: null,
      // 悬浮时行索引
      getHoverIndex: null,
      // 显示呢欧容
      dir_list: [],
      // 路径相关
      is_path: true,
      // 初始路径
      base_path: 'logs',
      // 当前路径
      log_path: '',
      // 显示路径 []
      display_path: [],
      // 前进一步
      history_path: '',
      // 历史操作记录， 后退记录，上一步
      history_path_list: [],
      // 前进记录，下一步
      back_path_list: [],
      // 文件内容
      display_file_status: false,
      file_text: '',
    }
  },
  mounted() {
    this.$nextTick(() => {
      // 提前获取详情
      open_dir(this.base_path).then(response => {
        this.dir_list = response.data.data
      })
      this.log_path = this.base_path
    })
  },
  methods: {

    //设置行对象(row)的样式(style)
    rowStyle({row, rowIndex}) {
      if (this.getIndex === rowIndex) {
        return {
          "background-color": "rgba(184,239,246,0.69)",
        };
      } else if (this.getHoverIndex === rowIndex) {
        return {
          "background-color": "rgba(184,239,246,0.37)",
        };
      }
    },
    //为行对象(row)设置index属性
    tableRowClassName({row, rowIndex}) {
      row.index = rowIndex;
    },
    //行点击事件,这里获取到的行对象(row)是没有index属性的
    rowClick(row) {
      this.getIndex = row.index;
    },
    // 悬浮时更改样式
    hoverStyle(row) {
      this.getHoverIndex = row.index;
    },

    // 失焦后显示路径
    focus_path() {
      this.is_path = false;
      this.$nextTick(() => {
        this.$refs.input.focus()
      })
    },
    // 聚焦后显示路径
    blur_path() {
      this.is_path = true;
      this.log_path = this.base_path
    },
    // enter 跳转
    onSubmit() {
      open_dir(this.log_path).then(response => {
        this.dir_list = response.data.data
      })
    },
    // 刷新
    refresh_content() {
      open_dir(this.log_path).then(response => {
        this.dir_list = response.data.data
      })
    },
    // 上一步
    onStep() {
      // 顶层目录
      if (!this.log_path) {
        this.log_path = this.base_path
        open_dir(this.log_path).then(response => {
          this.dir_list = response.data.data
        })
        return
      } else if (this.log_path === this.base_path) {
        open_dir(this.log_path).then(response => {
          this.dir_list = response.data.data
        })
        return
      }
      let _path = this.history_path_list.pop()
      this.back_path_list.push(_path)
      if (this.log_path === _path){
        this.onStep()
        return
      }
      this.log_path = _path
      open_dir(this.log_path).then(response => {
        this.dir_list = response.data.data
      })
    },
    // 下一步
    underStep() {
      let _path = this.back_path_list[0]
      if (!_path) {
        return
      }
      let path = this.back_path_list.pop()
      this.history_path_list.push(path)
      if (this.log_path === path) {
        this.underStep()
        return
      }
      this.log_path = path
      open_dir(this.log_path).then(response => {
        this.dir_list = response.data.data
      })
    },
    // 双击操作
    rowDblclick(row) {
      if (this.log_path === this.base_path) {
        this.history_path_list.push(this.log_path)
      }
      let path = this.log_path + '/' + row.name;
      if (row.type === '文本文档') {
        this.display_file_status = true
        open_file(path).then(response => {
          this.file_text = response.data.data
        })
      } else if (row.type === '文件夹') {
        this.history_path_list.push(path)
        this.log_path = path
        open_dir(path).then(response => {
          this.dir_list = response.data.data
        })
      }
    },
    // 删除
    my_delete(row) {
      let path = this.log_path + '/' + row.name;
      delete_file(path).then(response => {
        if (response.data.status === 'success') {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          // 设置延迟，实现组件刷新
          setTimeout(() => {
            this.refresh_content()
          }, 300)
        }
      })
    },
  },
  watch: {
    log_path: function () {
      this.display_path = this.log_path.split('/');
    }
  }

}
</script>

<style scoped>
.head {
  height: 40px;
}

/* 顶部栏 */
.step {
  float: left;
  width: 80px;
}

.step > span {
  line-height: 40px;
  text-align: center;
}

.step-i {
  width: 40px;
  font-size: 20px;
  color: #7e8188;
}

.step-i:hover {
  color: #0978e7c2;
}

.path {
  width: 55%;
  float: left;
  line-height: 40px;
}

.path_display {
  background-color: #FFF;
  background-image: none;
  border: 1px solid #DCDFE6;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: 14px;
  height: 40px;
  padding: 0 15px 0 4px;
  transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
}

/deep/ .el-input__inner {
  padding-left: 45px;
  border-radius: 0 !important;
}

.refresh {
  text-align: center;
  float: left;
  border-radius: 0;
}

.refresh > .el-button {
  text-align: center;
  padding: 0;
  width: 40px;
  line-height: 38px;
  font-size: 18px;
  color: #999b9d;
  border: 1px solid #DCDFE6;
  border-radius: 0
}

.refresh > .el-button:hover {
  color: #0978e7c2;
}

/* 内容 */

/* 悬浮行时， 背景持续1h内逐渐消失 */
/deep/ .el-table--enable-row-transition .el-table__body td.el-table__cell {
  transition: background-color 3600s linear;
}
</style>